package com.ayh.campusfun.common.core.config;

import com.ulisesbocchio.jasyptspringboot.annotation.EnableEncryptableProperties;
import org.jasypt.encryption.StringEncryptor;
import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.nio.charset.StandardCharsets;
import java.util.Base64;

/**
 * @author ayh
 * @create 2021/6/2 0002 20:51
 * jasytp加密配置文件
 */

@Configuration
@EnableEncryptableProperties
public class jasyptConfiguration {

    @Value("${jasypt.password}")
    private String jasyptPassword;

    @Bean("stringEncryptor")
    public StringEncryptor stringEncryptor() {
        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
        SimpleStringPBEConfig config = new SimpleStringPBEConfig();
        String secretKey = Base64.getEncoder().encodeToString(jasyptPassword.getBytes(StandardCharsets.UTF_8));
        // 这是加密的盐，建议直接硬编码，提高安全性
        config.setPassword(secretKey);
        // 加密算法
        config.setAlgorithm("PBEWithMD5AndDES");
        // key 迭代次数
        config.setKeyObtentionIterations("1000");
        // 池大小
        config.setPoolSize("1");
         // 提供方
//        config.setProviderName("SunJCE");
        // 随机盐生成器
//        config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
         // 加密后输出字符串编码方式
//        config.setStringOutputType("base64");
        encryptor.setConfig(config);
        return encryptor;
    }
}
